package com.neusoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.neusoft.base.BaseServiceImpl;
import com.neusoft.config.Pagination;
import com.neusoft.mapper.ItemMapper;
import com.neusoft.mapper.OrderItemRelMapper;
import com.neusoft.mapper.OrderMapper;
import com.neusoft.model.OrderSearch;
import com.neusoft.po.Item;
import com.neusoft.po.Order;
import com.neusoft.po.OrderItemRel;
import com.neusoft.po.Price;
import com.neusoft.service.ItemService;
import com.neusoft.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单服务实现
 *
 * @author Lucifer
 * @since 2021/5/16 8:10
 */
@Service
public class OrderServiceImpl extends BaseServiceImpl<Order> implements OrderService {


    @Resource
    private OrderMapper orderMapper;
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private OrderItemRelMapper orderItemRelMapper;
    @Resource
    private ItemService itemService;

    @Override
    public BaseMapper<Order> getBaseDao() {
        return orderMapper;
    }

    @Override
    public Order one(Integer order_id) {
        Order order = orderMapper.selectById(order_id);
        List<OrderItemRel> orderItemRels = orderItemRelMapper.selectList(new QueryWrapper<OrderItemRel>().eq("order_id", order_id));
        List<Integer> collect = orderItemRels.stream().map(OrderItemRel::getItem_id).collect(Collectors.toList());
        Integer[] ints = collect.toArray(new Integer[]{});
        List<Item> list = itemMapper.getWithComments(ints);
        order.setItems(list);
        order.setPrice(itemService.price(ints));
        return order;
    }

    @Override
    public Integer save(Order entity) {
        Integer id = entity.getId();
        Integer[] item_ids = entity.getItem_ids();
        if (null != item_ids && item_ids.length > 0) {
            Price price = itemService.price(item_ids);
            entity.setTotal(price.getReal_total());
        }
        if (null == id) {
            entity.setCreate_time(new Date());
            insert(entity);
            id = entity.getId();
        } else {
            updateById(entity);
            orderItemRelMapper.delete(new QueryWrapper<OrderItemRel>().in("item_id", item_ids));
        }
        for (Integer item_id : item_ids) {
            OrderItemRel orderItemRel = new OrderItemRel();
            orderItemRel.setItem_id(item_id);
            orderItemRel.setOrder_id(id);
            orderItemRelMapper.insert(orderItemRel);
        }
        return id;
    }

    @Override
    @Pagination
    public Object page(OrderSearch orderSearch) {
        List<OrderItemRel> orderItemRels = orderItemRelMapper.selectList(new QueryWrapper<OrderItemRel>().eq("order_id", orderSearch.getId()));
        List<Integer> collect = orderItemRels.stream().map(OrderItemRel::getItem_id).collect(Collectors.toList());
        Integer[] ints = collect.toArray(new Integer[]{});
        return itemMapper.getWithComments(ints);
    }

    @Override
    @Pagination
    public Object page2(OrderSearch orderSearch) {
        Page<Item> page = PageHelper.startPage(orderSearch.getPageNum(), orderSearch.getPageSize());
        List<Order> list = orderMapper.list(orderSearch);
        list.forEach(order -> {
            if (null != order.getItems() && order.getItems().size() > 0) {
                List<Integer> collect = order.getItems().stream().map(Item::getId).collect(Collectors.toList());
                List<String> names = order.getItems().stream().map(Item::getName).collect(Collectors.toList());
                Integer[] itemIds = collect.toArray(new Integer[]{});
                order.setPrice(itemService.price(itemIds));
                order.setItem_names(String.join(",", names));
            }
        });
        return list;
    }

    @Override
    public Integer submit(Integer id) {
        return orderMapper.submit(id);
    }

    @Override
    public Integer pay(Integer id) {
        return orderMapper.pay(id);
    }

    @Override
    public Integer delete(Integer[] ids) {
        orderItemRelMapper.delete(new QueryWrapper<OrderItemRel>().in("order_id", ids));
        return deleteByIds(ids);
    }
}
